package com.taysear.dbservice;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import com.jolbox.bonecp.BoneCP;
import com.jolbox.bonecp.BoneCPConfig;

/**
 * 
 * @author Abdullahi
 *
 */
public class DbService extends Ice.Application {
	private BoneCP vCP = null;

	@Override
	public int run(String[] args) {
		Ice.Properties pr = communicator().getProperties();

		// Load DataBase Connection pool
		if (loadDbParams(pr) < 0) {
			System.err.println("Failed To Load/Connect Database...");
			return 0;
		}
		Ice.ObjectAdapter adapter = communicator().createObjectAdapter( "DBSERVICEADAPTER");
		try {
			adapter.add(new DbServiceI(vCP), communicator().stringToIdentity("DBSERVICE"));
		} catch (Exception e1) {
			System.err.println(e1.getMessage());
		}
		adapter.activate();

		// communicator().waitForShutdown();
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		System.out.println("Db Service Started, Press q to quit");
		try {
			while (!in.readLine().trim().equals("q"));
		} catch (IOException e) {
			e.printStackTrace();
		}
		return 0;
	}

	/**
	 * Loads parameters from the configuration file. 
	 * 
	 * @param iceProperties
	 * @return int
	 */
	public int loadDbParams(Ice.Properties iceProperties) {
		System.out.println("::Starting Db Service::");
		// Read Database configuration properties

		String vDbDriver = iceProperties.getProperty("DBService.DBDriver");
		String vDbUrl = iceProperties.getProperty("DBService.DBUrl");
		String vDbUsername = iceProperties.getProperty("DBService.DBUsername");
		String vDbUserpwd = iceProperties.getProperty("DBService.DBUserpwd");
		int vDbPoolMin = Integer.parseInt(iceProperties.getProperty("DBService.DBPoolMin"));
		int vDbPoolMax = Integer.parseInt(iceProperties.getProperty("DBService.DBPoolMax"));
		int vDbPoolPartitions = Integer.parseInt(iceProperties.getProperty("DBService.DBPoolPartitions"));
		// Create Database Connection
		System.out.println("--- Loading Database Driver...");
		
		try {
			// load the database driver (make sure this is in your classpath!)
			Class.forName(vDbDriver);
		} catch (Exception e) {
			System.err.println("Failed TO Load the db driver");
			return -1;
		}
		System.out.println("--- Connecting to Database...");
		
		try {
			// setup the connection pool
			BoneCPConfig config = new BoneCPConfig();
			config.setJdbcUrl(vDbUrl); 
			config.setUsername(vDbUsername);
			config.setPassword(vDbUserpwd);
			config.setMinConnectionsPerPartition(vDbPoolMin);
			config.setMaxConnectionsPerPartition(vDbPoolMax);
			config.setPartitionCount(vDbPoolPartitions);
			
			//config.setIdleConnectionTestPeriodInMinutes(10);
			//config.setConnectionTestStatement("/* ping */ SELECT 1");
			
			vCP = new BoneCP(config); // setup the connection pool
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		}
		return 0;
	}

	/**
	 * Main method.
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		DbService obj = new DbService();
		int ret = obj.main("ServiceBroker", args);
		System.exit(ret);
	}
}
